//
//  MainViewController.m
//  iEasyTime
//


#import "MainViewController.h"
#import "StopwatchViewController.h" 
#import "CountdownViewController.h"
#import "ClockViewController.h"
#import "ClockViewEdit.h"
#import <QuartzCore/QuartzCore.h>

@implementation MainViewController 

@synthesize bar;
@synthesize swipeLeft;
@synthesize swipeRight;
//@synthesize tap;

@synthesize countdownViewController; 
@synthesize stopwatchViewController;
@synthesize clockViewController;
@synthesize clockViewEdit;

-(void)viewDidLoad { 
	[super viewDidLoad]; 
	
	swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
	swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
	[self.view addGestureRecognizer:swipeLeft];
	
	swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
	swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
	[self.view addGestureRecognizer:swipeRight];
	
//	tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
//	[self.view addGestureRecognizer:tap];
	
	
    clockViewController = [[ClockViewController alloc]  initWithNibName:@"ClockView" bundle:nil];  
    [self.view insertSubview:clockViewController.view atIndex:0]; 
	bar.topItem.title = @"Clock Screen";
	bar.tintColor = [UIColor blackColor];


} 

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)swipe{
	if(clockViewEdit.view.superview == nil){
		if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){
			[self goRight:nil];
		}else {
			[self goLeft:nil];
		}
	}
}

-(IBAction)goRight:(id)sender { 

	CATransition *animation = [CATransition animation];
	[animation setDuration:.5];
	[animation setType:kCATransitionPush];
	[animation setSubtype:kCATransitionFromRight];
	[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
	
    if (stopwatchViewController.view.superview != nil) 
    { 
        if (countdownViewController == nil) 
        { 
            countdownViewController = [[CountdownViewController alloc] initWithNibName:@"CountdownView" bundle:nil]; 

        }  
        [stopwatchViewController.view removeFromSuperview]; 
        [self.view insertSubview:countdownViewController.view atIndex:0];
		bar.topItem.title = @"Countdown Screen";
	} 
    else if(countdownViewController.view.superview != nil)
    { 
        if (self.clockViewController == nil) 
        { 
            clockViewController = [[ClockViewController alloc] initWithNibName:@"ClockView" bundle:nil]; 
        } 
        [countdownViewController.view removeFromSuperview]; 
        [self.view insertSubview:clockViewController.view atIndex:0]; 
			bar.topItem.title = @"Clock Screen";
    } 
	else if(clockViewController.view.superview != nil)
    { 
        if (self.stopwatchViewController == nil) 
        { 
            stopwatchViewController = [[StopwatchViewController alloc] initWithNibName:@"StopwatchView" bundle:nil]; 
        } 
        [clockViewController.view removeFromSuperview]; 
        [self.view insertSubview:stopwatchViewController.view atIndex:0];
		bar.topItem.title = @"Stopwatch Screen";
	}
	[[self.view layer] addAnimation:animation forKey:nil];	
}

-(IBAction)goLeft:(id)sender { 
	
	CATransition *animation = [CATransition animation];
	[animation setDuration:.5];
	[animation setType:kCATransitionPush];
	[animation setSubtype:kCATransitionFromLeft];
	[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
	
    if (clockViewController.view.superview != nil) 
    { 
        if (countdownViewController == nil) 
        { 
            countdownViewController = [[CountdownViewController alloc] initWithNibName:@"CountdownView" bundle:nil]; 
			
        }  
        [clockViewController.view removeFromSuperview]; 
        [self.view insertSubview:countdownViewController.view atIndex:0]; 
		bar.topItem.title = @"Countdown Screen";
	} 
    else if(stopwatchViewController.view.superview != nil)
    { 
        if (self.clockViewController == nil) 
        { 
            clockViewController = [[ClockViewController alloc] initWithNibName:@"ClockView" bundle:nil]; 
        } 
        [stopwatchViewController.view removeFromSuperview]; 
        [self.view insertSubview:clockViewController.view atIndex:0];
		bar.topItem.title = @"Temp Screen";
    } 
	else if(countdownViewController.view.superview != nil)
    { 
        if (self.stopwatchViewController == nil) 
        { 
            stopwatchViewController = [[StopwatchViewController alloc] initWithNibName:@"StopwatchView" bundle:nil]; 
        } 
        [countdownViewController.view removeFromSuperview]; 
        [self.view insertSubview:stopwatchViewController.view atIndex:0]; 
		bar.topItem.title = @"Stopwatch Screen";
	}
	[[self.view layer] addAnimation:animation forKey:nil];	
	
}

-(IBAction)goEdit:(id)sender {	
	
	if (self.clockViewEdit == nil) {
		clockViewEdit = [[ClockViewEdit alloc] initWithNibName:@"ClockViewEdit" bundle:nil];
	}

	UIView *mainView = clockViewController.view;
	UIView *flipsideView = clockViewEdit.view;
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1];
	[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
	
	if ([mainView superview] != nil) {
		[mainView removeFromSuperview];
		[self.view addSubview:flipsideView];
		bar.topItem.title = @"Clock Edit Screen";
		
	} else {
		[flipsideView removeFromSuperview];
		[self.view addSubview:mainView];
		bar.topItem.title = @"Clock Screen";
	}
	[UIView commitAnimations];
}
	
	


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
	// Release anything that's not essential, such as cached data 
    if (self.stopwatchViewController.view.superview == nil) 
        self.stopwatchViewController = nil; 
    else 
        self.countdownViewController = nil; 
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[countdownViewController release]; 
    [stopwatchViewController release];
	[clockViewController release];
	[clockViewEdit release];
    [super dealloc];
}


@end
